const { v4: uuidv4 } = require('uuid');
// const moment = require('moment');

/**
 * 表单模型
 */
module.exports = (app) => {
  const { STRING, INTEGER, TEXT,UUID } = app.Sequelize;
  const User = app.model.define(
    "user",
    {
      id: {
        type: UUID,
        unique: true,
        primaryKey: true,
        allowNull: false,
        defaultValue: () => {
          return uuidv4()
            .replace(/-/g, '');
        }
      },
      userName: {
        type: STRING,
        allowNull: false,
      },
      nickName: {
        type: STRING,
        allowNull: false,
      },
      headPicture: {
        type: TEXT,
        allowNull: false,
        defaultValue: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.alicdn.com%2Fi4%2F2204484777%2FO1CN01l3OVic1l9ufSylfAj_%21%212204484777.jpg&refer=http%3A%2F%2Fimg.alicdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1675046478&t=2f817922272f32dad56eb18044afe38f',
      },
      disabled: {
        type: INTEGER,
        allowNull: false,
        defaultValue: 0,
      },
      delFlag: {
        type: INTEGER,
        allowNull: false,
        defaultValue: 0,
      },
    },
    {
      timestamps: false,
    }
  );

  // 表关联的字段
  User.associate = function () {

    // 一对一
    app.model.User.belongsTo(app.model.Account, {
      foreignKey: 'account_id',
      targetKey: 'id',
      as: 'account',
  })


    // 一对多
    app.model.User.hasMany(app.model.Note, {
      foreignKey: "user_id",
      targetKey: "id",
    });

    // 一对多
    app.model.User.hasMany(app.model.Video, {
      foreignKey: "user_id",
      targetKey: "id",
    });

     //多对多
    app.model.User.belongsToMany(app.model.Role, {
      through: app.model.UserRole,
      foreignKey: 'userId',
      otherKey: 'roleId',
    })
    

  };

  return User;
};
